package com.china.zhuoyue.ui.dialog

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.fragment.app.Fragment
import com.china.zhuoyue.ui.R
import com.china.zhuoyue.ui.databinding.DialogAlertBinding
import com.china.zhuoyue.ui.ext.getString
import com.china.zhuoyue.ui.ext.gone

private typealias callback = (() -> Unit)?

class AlertDialog(context: Context) : Dialog(context, R.style.dimLoadingdialog),
    View.OnClickListener {
    private lateinit var viewBinding: DialogAlertBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        viewBinding = DialogAlertBinding.inflate(LayoutInflater.from(context))
        setContentView(viewBinding.root)
        viewBinding.apply {
            (titleStr ?: R.string.title_dialog_tips.getString()).let { title.text = it }
            messageStr?.let { message.text = it }
            if (positiveButtonCallback != null && positiveButtonName.isNullOrEmpty()) positiveButtonName =
                R.string.button_confirm.getString()
            if (!positiveButtonName.isNullOrEmpty()) {
                positiveButton.apply {
                    text = positiveButtonName
                    setOnClickListener(this@AlertDialog)
                }
            } else positiveButton.gone()
            if (negativeButtonCallback != null && negativeButtonName.isNullOrEmpty()) negativeButtonName =
                R.string.button_cancel.getString()
            if (!negativeButtonName.isNullOrEmpty()) {
                negativeButton.apply {
                    text = negativeButtonName
                    setOnClickListener(this@AlertDialog)
                }
            } else negativeButton.gone()
            setCancelable(mCancelable)
            window?.attributes?.run {
                width = (context.resources.displayMetrics.widthPixels * 0.85).toInt()
            }
        }
    }

    override fun onClick(v: View?) {
        when (v) {
            viewBinding.positiveButton -> v.let { if (positiveDismissAfterClick) dismiss();positiveButtonCallback?.invoke() }
            viewBinding.negativeButton -> v.let { if (negativeDismissAfterClick) dismiss();negativeButtonCallback?.invoke() }
        }
    }

    private var titleStr: CharSequence? = null
    private var messageStr: CharSequence? = null
    private var positiveButtonName: CharSequence? = null
    private var positiveButtonCallback: callback = null
    private var positiveDismissAfterClick: Boolean = true
    private var negativeButtonName: CharSequence? = null
    private var negativeButtonCallback: callback = null
    private var negativeDismissAfterClick: Boolean = true
    var mCancelable: Boolean = true


    fun title(titleRes: Int = 0, title: CharSequence = "") {
        titleStr = if (titleRes != 0) titleRes.getString() else title
    }

    fun message(messageRes: Int = 0, message: CharSequence = "") {
        messageStr = if (messageRes != 0) messageRes.getString() else message
    }

    fun positiveButton(
        nameRes: Int = 0,
        name: CharSequence = "",
        dismissAfterClick: Boolean = positiveDismissAfterClick,
        callback: callback = null
    ) {
        positiveButtonName = if (nameRes != 0) nameRes.getString() else name
        positiveDismissAfterClick = dismissAfterClick
        positiveButtonCallback = callback
    }

    fun negativeButton(
        nameRes: Int = 0,
        name: CharSequence = "",
        dismissAfterClick: Boolean = negativeDismissAfterClick,
        callback: callback = null
    ) {
        negativeButtonName = if (nameRes != 0) nameRes.getString() else name
        negativeDismissAfterClick = dismissAfterClick
        negativeButtonCallback = callback
    }
}

fun Activity?.showAlertDialog(settings: AlertDialog.() -> Unit) =
    this?.let { AlertDialog(this).also(settings).show() }

fun Fragment?.showAlertDialog(settings: AlertDialog.() -> Unit) =
    this?.let { activity?.let { AlertDialog(it).also(settings).show() } }